Lecture 1 & 2 - Practice
Q1: Properties
اعمل Facts للجمل الانجليزي دي:
- Gold is valuable.
- Saly is a female.
- Aly owns some gold.
- Gamal gives the book to Mona. (3 arguments)
Solution (Properties)
valuable(gold).
female(saly).
owns(aly, gold).
gives(gamal, book, mona).
Q2: Facts Translation
اعمل Facts لجمل الانجليزي دي:
- A car is fun.
- A rose is red.
- Bill likes Cindy.
- Cindy likes Bill.
- Bill likes dogs.
Solution (Facts Translation)
fun(car).
red(rose).
likes(bill, cindy).
likes(cindy, bill).
likes(bill, dogs).
Q3: Age Database
اعمل قاعدة بيانات للداتا دي:
- Aly is 42 years old.
- Hany is 15 years old.
- Saly is 20 years old.
- Aly is male.
- Hany is male.
- Saly is f1.emale.
واكتب الاستعلامات دي:
- Is Aly 42 years old?
- What is Hany's age?
- Who is male? (اعرض كل الإجابات)
- List names with age < 40.
- List names with age < 40 and they are female.
Solution (Age Database)
age(aly, 42).
age(hany, 15).
age(saly, 20).
male(aly).
male(hany).
female(saly).
Queries:
?- age(aly, 42). % true
?- age(hany, X). % X = 15
?- male(Y). % Y = hany ; Y = aly
?- age(Name, Age), Age < 40.
% Name = hany, Age = 15 ;
% Name = saly, Age = 20
?- age(Name, Age), Age < 40, female(Name).
% Name = saly, Age = 20
Q4: Love Database 💕💕
اعمل قاعدة بيانات بالـ Facts دي:
- Mia is a woman.
- Jana is a woman.
- Yara is a woman.
- Fady loves Mia.
- Magdy loves Mia.
- Magdy loves Badr.
- Aly loves Badr.
- Hany loves Aly.
واكتب الاستعلامات:
- Who is a woman? (اعرض كل النتائج واحدة واحدة)
- Does Magdy love Mia?
- Does Magdy love Badr?
Solution (Loves Database)
woman(mia).
woman(jana).
woman(yara).
loves(fady, mia).
loves(magdy, mia).
loves(magdy, badr).
loves(aly, badr).
loves(hany, aly).
Queries:
?- woman(X).
% X = mia ; X = jana ; X = yara ; no
?- loves(magdy, mia). % yes
?- loves(magdy, badr). % yes
Q5: Conjunctions
من قاعدة البيانات:
likes(mary, food).
likes(mary, tea).
likes(john, tea).
likes(john, mary).
اكتب الاستعلامات دي باستخدام الـ conjunctions ا ( and , or ):
- Does John like Mary and does Mary like John?
- Is there anything that John and Mary both like?
Solution (Conjunctions)
?- likes(john, mary), likes(mary, john).
% no :(
?- likes(mary, X), likes(john, X).
% X = tea
Q6: Jim's Family
اعمل قاعدة بيانات لعيلة Jim:
- John is the father of Jim.
- Jane is the mother of Jim.
- Jack is father of John.
واكتب الاستعلامات:
- Who is Jim's father?
- Is Jane the mother of Fred?
- Is Jane the mother of Jim?
- Does Jack have a grandchild?
Solution (Jim's Family)
father(john, jim).
mother(jane, jim).
father(jack, john).
Queries:
?- father(X, jim).
?- mother(jane, fred).
?- mother(jane, jim).
?- father(jack, X), father(X, Y).
Q7: Charles's Family1.
اعمل قاعدة بيانات لعيلة Charles:
- Charles is male.
- Edward is male.
- Philip is male.
- Anne is female
- Philip is a parent of Anne
- Philip is a parent of Edward
- Philip is a parent of Charles
واكتب الاستعلامات دي:
- The parents of Charles.
- The father of Charles.
- The grandfather of Charles.
- All the grandchildren of the grandparents of Charles. ( احفاد جد تشارلز )
- All males.
Solution (Charles's Family)
?- parent(X, charles).
?- parent(X, charles),male(X).
?- parent(X, charles), parent(Y, X).
?- parent(X, charles), parent(Y, X), parent(Y, P), parent(P, G).
?- male(X).
Q8: Pam & Bob Family Tree
استخدم قاعدة بيانات لشجرة عيلة Pam و Bob :
parent(pam, bob).
parent(tom, bob).
parent(tom, liz).
parent(bob, ann).
parent(bob, pat).
parent(pat, jim).
واكتب الاستعلامات:
- Who is Liz's parent?
- Who are Bob's children?
- Who is a parent of whom?
- Who is a grandparent of Jim?.
- Do Ann and Pat have a common parent?
Solution (Extended Pam & Bob)
Queries:
?- parent(X, liz).
?- parent(bob, X).
?- parent(X, Y).
?- parent(X, Y), parent(Y, jim).
?- parent(X, ann), parent(X, pat).